package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.CType;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Smesten' from the chart.
 * 
 * @author Lab
 * 
 */
public class Repository extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_DEPOT = "depot";
    public static final String ONT_PLACE = "place";
    public static final String ONT_APPELATION = "appelation";

    public Repository(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null) {
            return;
        }

        Ont.typeCheck(CType.DEPOT);

        final Query query = new Query();
        final String[] result = query.select("place").type("place", CIDOC.E53.queryValue())
                .type("placeApp", CIDOC.E44.queryValue()).label("placeApp", this.value)
                .connect("place", CIDOC.P87.queryValue(), "placeApp")
                .connect("place", CIDOC.P2.queryValue(), CType.DEPOT.queryValue()).find();

        final String itemURI = Item.formatItemURI(this.id);
        String placeURI;
        if (result == null || result.length == 0) {
            placeURI = Ont.formatURI(this.id, ONT_PLACE, ONT_DEPOT);
            final String placeAppURI = Ont.formatURI(this.id, ONT_PLACE, ONT_APPELATION, ONT_DEPOT);

            Ont.getOntology().createIndividual(CIDOC.E53.getFullValue(), placeURI);
            Ont.getOntology().createIndividual(CIDOC.E44.getFullValue(), placeAppURI);

            Ont.getOntology().addLabel(placeAppURI, this.value);

            Ont.getOntology().connectIndividuals(placeURI, CIDOC.P2.getFullValue(), CType.DEPOT.getURI());
            Ont.getOntology().connectIndividuals(placeURI, CIDOC.P87.getFullValue(), placeAppURI);
        } else {
            placeURI = result[0];
        }

        Ont.getOntology().connectIndividuals(itemURI, CIDOC.P55.getFullValue(), placeURI);
    }

    public void fromRDF() {
        final String itemQueryValue = Item.formatItemQueryURI(this.id);
        final Query query = new Query();
        final String[] result = query.selectLabel().type("place", CIDOC.E53.queryValue())
                .connect(itemQueryValue, CIDOC.P55.queryValue(), "place").type("placeName", CIDOC.E44.queryValue())
                .connect("place", CIDOC.P87.queryValue(), "placeName")
                .connect("place", CIDOC.P2.queryValue(), CType.DEPOT.queryValue()).findLabel("placeName");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.value != null) {
            doc.add(SearchMD.CONTENT.createField(this.value));
        }
    }
}
